package com.wisely.spring_boot_security.dao.user;

import com.wisely.spring_boot_security.dao.IUserDao;
import com.wisely.spring_boot_security.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class UserDaoImpl implements IUserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public SysUser getUserByName(String name) {
        String sql = "select * from SYS_USER where username = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SysUser.class), name);
    }

    @Override
    public List<Map<String, Object>> listSysUserRolesByUserid(Long id) {
        String sql = "select ur.roles_id roleId,r.name roleName from SYS_USER_ROLES ur,SYS_ROLE r where r.id = ur.roles_id AND sys_user_id = ?";
        return jdbcTemplate.queryForList(sql, id);
    }
}

